package com.xxx.lease.web.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxx.lease.common.exception.LeaseException;
import com.xxx.lease.common.result.ResultCodeEnum;
import com.xxx.lease.model.entity.*;
import com.xxx.lease.model.enums.ItemType;
import com.xxx.lease.web.admin.mapper.*;
import com.xxx.lease.web.admin.service.*;
import com.xxx.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.xxx.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.xxx.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.xxx.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.xxx.lease.web.admin.vo.fee.FeeValueVo;
import com.xxx.lease.web.admin.vo.graph.GraphVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
* @author zlz
* @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
* @createDate 2024-09-19 20:00:29
*/
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
    implements ApartmentInfoService{

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;

    @Autowired
    private FacilityInfoMapper facilityInfoMapper;

    @Autowired
    private LabelInfoMapper labelInfoMapper;

    @Autowired
    private FeeValueMapper feeValueMapper;

    @Autowired
    private GraphInfoMapper graphInfoMapper;

    @Autowired
    private RoomInfoMapper roomInfoMapper;

    @Autowired
    private ProvinceInfoMapper provinceInfoMapper;

    @Autowired
    private CityInfoMapper cityInfoMapper;

    @Autowired
    private DistrictInfoMapper districtInfoMapper;


    //以下使用service的原因是在service中有批量处理的API
    @Autowired
    private ApartmentLabelService apartmentLabelService;

    @Autowired
    private ApartmentFacilityService apartmentFacilityService;

    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService;

    @Autowired
    private GraphInfoService graphInfoService;


    @Override
    public IPage<ApartmentItemVo> pageItem(Page<ApartmentItemVo> apartmentItemVoPage, ApartmentQueryVo queryVo) {
        return apartmentInfoMapper.pageItem(apartmentItemVoPage, queryVo);
    }

    @Override
    public ApartmentDetailVo getDetailById(Long id) {

        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        //查询公寓的基本信息
        ApartmentInfo apartmentInfo = this.getById(id);

        BeanUtils.copyProperties(apartmentInfo, apartmentDetailVo);

        //查询公寓的配套信息
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectListByApartmentId(id);


        //查询公寓的图片
        List<GraphVo> graphVoList = graphInfoMapper.selectListByIdAndType(id, ItemType.APARTMENT);


        //查询公寓的标签
        List<LabelInfo> labelInfoList = labelInfoMapper.selectListByApartmentId(id);



        //查询公寓的杂费
        List<FeeValueVo> feeValueVoList = feeValueMapper.selectListByApartmentId(id);


        apartmentDetailVo.setLabelInfoList(labelInfoList);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);
        apartmentDetailVo.setGraphVoList(graphVoList);
        apartmentDetailVo.setFeeValueVoList(feeValueVoList);


        return apartmentDetailVo;
    }

    @Override
    public void removeApartmentById(Long id) throws LeaseException {

        //判断该公寓下是否有房间
        if (roomInfoMapper.selectCount(new LambdaQueryWrapper<RoomInfo>().eq(RoomInfo::getApartmentId, id)) > 0) {
            throw new LeaseException(ResultCodeEnum.DELETE_ERROR);
        }



        //删除公寓的图片
        LambdaQueryWrapper<GraphInfo> graphVoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        graphVoLambdaQueryWrapper.eq(GraphInfo::getItemId, id);
        graphVoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
        graphInfoMapper.delete(graphVoLambdaQueryWrapper);

        //删除公寓的标签
        apartmentLabelService.remove(new LambdaQueryWrapper<ApartmentLabel>().eq(ApartmentLabel::getApartmentId, id));

        //删除公寓的配套信息
        apartmentFacilityService.remove(new LambdaQueryWrapper<ApartmentFacility>().eq(ApartmentFacility::getApartmentId, id));

        //删除公寓的杂费
        apartmentFeeValueService.remove(new LambdaQueryWrapper<ApartmentFeeValue>().eq(ApartmentFeeValue::getApartmentId, id));

        //删除公寓的基本信息
        super.removeById(id);
    }

    @Override
    public void saveOrUpdateInfo(ApartmentSubmitVo apartmentSubmitVo) {

        //获取公寓的id
        Long id = apartmentSubmitVo.getId();

        //根据provinceId查询省份的信息
        ProvinceInfo provinceInfo = provinceInfoMapper.selectById(apartmentSubmitVo.getProvinceId());
        apartmentSubmitVo.setProvinceName(provinceInfo.getName());

        //查询城市信息
        CityInfo cityInfo = cityInfoMapper.selectById(apartmentSubmitVo.getCityId());
        apartmentSubmitVo.setCityName(cityInfo.getName());

        //查询地区信息
        DistrictInfo districtInfo = districtInfoMapper.selectById(apartmentSubmitVo.getDistrictId());
        apartmentSubmitVo.setDistrictName(districtInfo.getName());

        //添加或者修改公寓的基本信息
        super.saveOrUpdate(apartmentSubmitVo);

        //判断是修改（apartmentSubmitVo中有公寓的id）还是添加
        if (id != null){

            //删除公寓的图片
            LambdaQueryWrapper<GraphInfo> graphVoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            graphVoLambdaQueryWrapper.eq(GraphInfo::getItemId, id);
            graphVoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
            graphInfoService.remove(graphVoLambdaQueryWrapper);

            //删除公寓的标签
            apartmentLabelService.remove(new LambdaQueryWrapper<ApartmentLabel>().eq(ApartmentLabel::getApartmentId, id));

            //删除公寓的配套信息
            apartmentFacilityService.remove(new LambdaQueryWrapper<ApartmentFacility>().eq(ApartmentFacility::getApartmentId, id));

            //删除公寓的杂费
            apartmentFeeValueService.remove(new LambdaQueryWrapper<ApartmentFeeValue>().eq(ApartmentFeeValue::getApartmentId, id));

        }

        //添加 关系表中新的信息

        List<Long> facilityInfoIdList = apartmentSubmitVo.getFacilityInfoIds();

        if (!CollectionUtils.isEmpty(facilityInfoIdList)) {
            List<ApartmentFacility> apartmentFacilityList = new ArrayList<>();

            for (Long facilityInfoId : facilityInfoIdList) {
                ApartmentFacility apartmentFacility = ApartmentFacility.builder().build();
                apartmentFacility.setApartmentId(apartmentSubmitVo.getId());
                apartmentFacility.setFacilityId(facilityInfoId);

                apartmentFacilityList.add(apartmentFacility);
            }
            apartmentFacilityService.saveBatch(apartmentFacilityList);
        }


        List<Long> labelIdList = apartmentSubmitVo.getLabelIds();

        if (!CollectionUtils.isEmpty(labelIdList)) {

            List<ApartmentLabel> apartmentLabelList = new ArrayList<>();

            for (Long labelId : labelIdList) {
                ApartmentLabel apartmentLabel = ApartmentLabel.builder().build();
                apartmentLabel.setApartmentId(apartmentSubmitVo.getId());
                apartmentLabel.setLabelId(labelId);

                apartmentLabelList.add(apartmentLabel);
            }
            apartmentLabelService.saveBatch(apartmentLabelList);
        }

        List<Long> feeValueIdList = apartmentSubmitVo.getFeeValueIds();

        if (!CollectionUtils.isEmpty(feeValueIdList)) {

            List<ApartmentFeeValue> apartmentFeeValueList = new ArrayList<>();

            for (Long feeValueId : feeValueIdList) {
                ApartmentFeeValue apartmentFeeValue = ApartmentFeeValue.builder().build();
                apartmentFeeValue.setApartmentId(apartmentSubmitVo.getId());
                apartmentFeeValue.setFeeValueId(feeValueId);
                apartmentFeeValueList.add(apartmentFeeValue);
            }
            apartmentFeeValueService.saveBatch(apartmentFeeValueList);
        }

        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();

        if (!CollectionUtils.isEmpty(graphVoList)) {

            List<GraphInfo> graphInfoList = new ArrayList<>();

            for (GraphVo graphVo : graphVoList) {

                GraphInfo graphInfo = new GraphInfo();

                graphInfo.setItemId(apartmentSubmitVo.getId());

                graphInfo.setItemType(ItemType.APARTMENT);

                graphInfo.setUrl(graphVo.getUrl());

                graphInfo.setName(graphVo.getName());

                graphInfoList.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfoList);
        }


    }
}




